package com.bjsxt.shop.head.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bjsxt.shop.common.controller.BaseController;
import com.bjsxt.shop.common.util.ConstatFinalUtil;
import com.bjsxt.shop.users.pojo.AUsers;
import com.bjsxt.shop.users.pojo.AUsersAddress;
import com.bjsxt.shop.users.pojo.AUsersAddressEnum;
import com.bjsxt.shop.users.pojo.AUsersCar;
import com.bjsxt.shop.users.service.IUsersService;

/**
 * 管理员登陆后的Controller
 * 
 * @author wangshSxt
 *
 */
@Controller
@RequestMapping("/head/orders")
public class OrdersHeadController extends BaseController
{
	@Resource
	private IUsersService usersService;
	
	/**
	 * 订单确认
	 * 
	 * @return
	 */
	@RequestMapping("/ordersConfirm")
	public String ordersConfirm(HttpServletRequest request)
	{
		ConstatFinalUtil.LOGGER.info("---- ordersConfirm ----");
		HttpSession session = request.getSession() ;
		AUsers users = (AUsers) session.getAttribute("users");
		
		/* 查询用户的所有收货地址(启用) */
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("usersid",users.getId());
		condMap.put("status", AUsersAddressEnum.STATUS_ENABLE.getStatus());
		List<AUsersAddress> usersAddressList = this.usersService.findCondListUsersAddressService(null, condMap);
		request.setAttribute("usersAddressList", usersAddressList);
		
		/* 获取选中的购物车 */
		String[] shids = request.getParameterValues("shids");
		if(shids != null)
		{
			/**
			 * 查询选中的购物车中的商品
			 */
			List<AUsersCar> usersCarList = new ArrayList<AUsersCar>() ; 
			for (int i = 0; i < shids.length; i++)
			{
				String shid = shids[i];
				/* 根据Id查询数据库里面的购物车 */
				condMap.clear();
				condMap.put("id", shid);
				AUsersCar usersCar = this.usersService.findOneUsersCarService(condMap);
				usersCarList.add(usersCar);
			}
			request.setAttribute("usersCarList", usersCarList);
		}
		
		return "/head/orders/ordersConfirm";
	}
}
